package com.spartez.farhorizons.client.map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.gwtext.client.util.Format;
import com.spartez.farhorizons.client.Calculator;
import com.spartez.farhorizons.client.FarHorizons;
import com.spartez.farhorizons.client.MapPanel;
import com.spartez.farhorizons.client.DataService.SystemSelectedListener;
import com.spartez.farhorizons.client.data.ISystemDescr;

public class SystemComponent implements MouseOverHandler, MouseOutHandler, ClickHandler, SystemSelectedListener {
	private final ISystemDescr systemDescr;
	
	private final InlineHTML coordLabel;
	private final InlineHTML typeLabel;
	
	private boolean selected;
	
	public SystemComponent(ISystemDescr sysDescr, HTMLPanel container, boolean compact) {
		this.systemDescr = sysDescr;
		coordLabel = new InlineHTML(MapPanel.formatCell(String.valueOf(sysDescr.getZ()), compact));
		typeLabel = new InlineHTML(MapPanel.formatCell(sysDescr.getType(), compact));
		
		coordLabel.setWordWrap(false);
		typeLabel.setWordWrap(false);
		
		coordLabel.addMouseOverHandler(this);
		typeLabel.addMouseOverHandler(this);

		coordLabel.addMouseOutHandler(this);
		typeLabel.addMouseOutHandler(this);

		coordLabel.setTitle(makeTitle());
		typeLabel.setTitle(makeTitle());
		
		coordLabel.addClickHandler(this);
		typeLabel.addClickHandler(this);
		
		FarHorizons.dataService.registerSystemSelectedListener(this);
		
		final String typeId = "type_" + sysDescr.getX() + "_" + sysDescr.getY();
		final String coordId = "zcoord_" + sysDescr.getX() + "_" + sysDescr.getY();
		container.addAndReplaceElement(typeLabel, typeId);
		container.addAndReplaceElement(coordLabel, coordId);

		if (FarHorizons.dataService.getSelectedSystem() == sysDescr) {
			selected = true;
			restoreBackground();
		}
	}
	
	private String makeTitle() {
		StringBuilder sb = new StringBuilder(Format.format("System at {0} {1} {2}", systemDescr.getX(), systemDescr.getY(), systemDescr.getZ()));
		ISystemDescr selected = FarHorizons.dataService.getSelectedSystem();
		if (selected != null && selected != systemDescr) {
			sb.append('\n');
			sb.append(Format.format("Distance from {0} {1} {2} is ", selected.getX(), selected.getY(), selected.getZ()));
			sb.append(Calculator.calcDistance(selected, systemDescr));
		}
		return sb.toString();
	}

	public void onMouseOver(MouseOverEvent event) {
		if (!selected) {
			coordLabel.getElement().getStyle().setProperty("backgroundColor", "FFFF88");
			typeLabel.getElement().getStyle().setProperty("backgroundColor", "FFFF88");
			
			FarHorizons.dataService.setHoveredSystem(systemDescr);
		}
	}

	public void onMouseOut(MouseOutEvent event) {
		restoreBackground();
	}

	private void restoreBackground() {
		final String color;
		if (selected) {
			color = "8888FF";
		} else {
			color = null;
		}
		coordLabel.getElement().getStyle().setProperty("backgroundColor", color);
		typeLabel.getElement().getStyle().setProperty("backgroundColor", color);
	}

	public void onClick(ClickEvent event) {
		selected = true;
		restoreBackground();
		FarHorizons.dataService.setSelectedSystem(systemDescr);
	}

	public void systemSelected(ISystemDescr selectedSystem) {
		if (selectedSystem != systemDescr) {
			selected = false;
			restoreBackground();
			final String newTitle = makeTitle();
			coordLabel.setTitle(newTitle);
			typeLabel.setTitle(newTitle);
		}
	}
}
